//
//  CWVisitorLoginView.swift
//  新浪微博练习02
//
//  Created by 曹魏 on 16/9/25.
//  Copyright © 2016年 itcast. All rights reserved.
//访客视图界面

import UIKit
import SnapKit
//制订协议
@objc protocol CWVisitorLoginViewDelegate : NSObjectProtocol {
    //协议方法
    //登录
    func userWillLogin()
    
    //注册
    @objc optional func userWillRegister()
    
}



class CWVisitorLoginView: UIView {
    //声明一个代理对象属性,需要使用weak修饰,避免造成循环引用
    weak var delegate : CWVisitorLoginViewDelegate?
    
    
    
    //对外提供外界设置信息的方法
    func updateInfo(tipText : String,imageName : String? = nil) {
        tipLabel.text = tipText
        if (imageName == nil) {
            //是首页
            //执行动画效果
            startAnimation()
            
            
        }else{
            //不是首页,隐藏首页图片
            circleView.image = UIImage(named: imageName!)
            iconView.isHidden = true
            backView.isHidden = true
            //也可以利用一个方法把circleView放到上面
//            bringSubview(toFront: circleView)
        }
        
        
        
    }
    private func startAnimation() {
        //1.使用基础动画
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        //2.设置动画的相关参数
        //设置重复次数
        animation.repeatCount = MAXFLOAT
        //3.设置一个tovalue 一圈
        animation.toValue = 2 * M_PI
        //4.设置动画时间
        animation.duration = 15
        //5.当动画结束的时候或者当前页面处于非活跃状态时,默认是移除动画的,所以,设置为不移除动画
        animation.isRemovedOnCompletion = false
        
        //6.将动画效果添加到圈圈图层上
        circleView.layer.add(animation, forKey: nil)
    }
    
    
    
    
    
    
    //自定义访客视图,添加子视图
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        setUpUI()
        
        
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //设置UI 界面
    func setUpUI() {
        addSubview(circleView)
        addSubview(backView)
        addSubview(iconView)
        addSubview(tipLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        //利用第三方框架来对控件进行约束布局
        //1.转动的圈圈
        circleView.snp.makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(-80)
        }
        //2.大房子
        iconView.snp.makeConstraints { (make) in
            make.center.equalTo(circleView)
        }
        //3.遮照
        backView.snp.makeConstraints { (make) in
            make.center.equalTo(circleView)
        }
        //4.文字
        tipLabel.snp.makeConstraints { (make) in
            make.top.equalTo(circleView.snp.bottom).offset(16)
            make.centerX.equalTo(self)
        }
        //5.注册按钮
        registerBtn.snp.makeConstraints { (make) in
            make.top.equalTo(tipLabel.snp.bottom).offset(16)
            make.right.equalTo(tipLabel)
            make.width.equalTo(100)
        }
        //6.登录按钮
        loginBtn.snp.makeConstraints { (make) in
            make.top.equalTo(tipLabel.snp.bottom).offset(16)
            make.left.equalTo(tipLabel)
            make.width.equalTo(100)
        }
        let value = CGFloat(237/255.0)
        
        backgroundColor = UIColor(red: value, green: value, blue: value, alpha: 1.0)
        //添加按钮监听点击事件
        //登录点击
        loginBtn.addTarget(self, action: #selector(loginBtnDidClick), for: .touchUpInside)
        //注册点击
        registerBtn.addTarget(self, action: #selector(registerBtnDidClick), for: .touchUpInside)
    }
    @objc private func loginBtnDidClick() {
        //使用代理对象实现协议方法
        delegate?.userWillLogin()
    }
    @objc private func registerBtnDidClick() {
        //可选的协议方法,可以实现也可以不实现
        //必选的协议方法,必须要实现
        delegate?.userWillRegister?()
    }
    
    //懒加载UI控件
    //1.转动的圈圈
    private lazy var circleView : UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_smallicon"))
    //2.大房子
    private lazy var iconView : UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_house"))
    //3.遮照
    private lazy var backView : UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_mask_smallicon"))
    //4.文字
    private lazy var tipLabel : UILabel = {
        let l = UILabel(title: "关注一些人,快回到这里看看有什么惊喜哈哈哈哈哈哈哈哈", textColor: UIColor.darkGray, fontSize: 14)
//        l.sizeToFit()
        //设置自动换行
        l.numberOfLines = 0
        //设置最大的布局宽度
        l.preferredMaxLayoutWidth = 224
        //设置文字居中
        l.textAlignment = .center
        return l
        
    }()
    //5.注册按钮
    private lazy var registerBtn : UIButton = {
        let btn = UIButton(title: "注册", textColor: UIColor.orange, fontSize: 14)
        //设置背景图片
        btn.setBackgroundImage(#imageLiteral(resourceName: "common_button_white"), for: .normal)
        
        
        
        return btn
        
    }()
    //6.登录按钮
    private lazy var loginBtn : UIButton = {
        let btn = UIButton(title: "登录", textColor: UIColor.darkGray, fontSize: 14)
        //设置背景图片
        btn.setBackgroundImage(#imageLiteral(resourceName: "common_button_white"), for: .normal)
        
        
        
        return btn
        
    }()
    
    
    
    
    
    
    
    

}




























